const proj4 = require('proj4');

// 定义 WGS84 和 UTM49N 坐标系
proj4.default.defs("EPSG:4326", "+proj=longlat +datum=WGS84 +no_defs");
proj4.default.defs("EPSG:32649", "+proj=utm +zone=49 +datum=WGS84 +units=m +no_defs");
proj4.default.defs("EPSG:4547", "+proj=tmerc +lat_0=0 +lon_0=114 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs +type=crs");
proj4.default.defs("EPSG:4544","+proj=tmerc +lat_0=0 +lon_0=105 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs");

// 经纬度转 UTM49N
export function convertToUTM49N(lon, lat,espgCode) {
    // 将经纬度从 WGS84 转换为 UTM49N
    const utm = proj4.default("EPSG:4326", "EPSG:"+espgCode, [lon, lat]);
    return [
        utm[0], // 东坐标
        utm[1], // 北坐标
    ];
}

export function calculateCenter(geoTransform, width, height) {
    const [gt0, gt1, gt2, gt3, gt4, gt5] = geoTransform;

    const centerCol = width / 2;
    const centerRow = height / 2;

    const centerX = gt0 + centerCol * gt1 + centerRow * gt2;
    const centerY = gt3 + centerCol * gt4 + centerRow * gt5;

    return [centerX, centerY];
}

// UTM49N 转经纬度
export function convertToLatLon(easting, northing,espgCode) {
    // 将 UTM49N 坐标转换为 WGS84 经纬度
    const lonlat = proj4.default("EPSG:"+espgCode, "EPSG:4326", [easting, northing]);
    return [
        lonlat[0],
        lonlat[1]
    ];
}
